<template>
  <el-form
    ref="userForm"
    :model="userInfo"
    label-width="120px"
    :rules="rules"
    style="margin-left: 120px; margin-top: 30px"
  >
    <el-form-item label="姓名:" prop="username">
      <el-input v-model="userInfo.username" style="width:300px" />
    </el-form-item>
    <el-form-item label="密码:" prop="password">
      <el-input v-model="userInfo.password" type="password" style="width:300px" />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="hSaveFn">更新</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailByIdAPI } from '@/api/employees'
export default {
  data() {
    return {
      rules: {
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ]
      },
      userInfo: {
        id: this.$route.query.id,
        username: '',
        password: ''
      }
    }
  },
  created() {
    this.getUserDetail()
  },
  methods: {
    async getUserDetail() {
      const res = await getUserDetailById(this.userInfo.id)
      console.log('请求回来的数据login', res)
      this.userInfo = res
    },
    async doSaveFn() {
      try {
        const res = await saveUserDetailByIdAPI(this.userInfo)
        console.log('更新之后后端返回的数据', res)
        this.$message.success('更新成功')
        this.$router.back()
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    hSaveFn() {
      this.$refs.userForm.validate(valid => {
        valid && this.doSaveFn()
      })
    }
  }
}
</script>

<style>

</style>
